﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Xml;

namespace curbside.Core.Utility.Serialize
{
    public class SerializeHelper
    {
        #region Json Serialize

        /// <summary>
        /// 将对象序列化为Json格式
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string JsonSerialize(object data)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, data);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
        /// <summary>
        /// 将Json反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }

        #endregion

        #region Binary Serialize

        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="objBytes">需要进行反序列化的二进制数据</param>
        /// <returns></returns>
        public static T BinaryDeserialize<T>(byte[] objBytes)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                memoryStream.Write(objBytes, 0, objBytes.Length);
                memoryStream.Position = 0;
                return (T)formatter.Deserialize(memoryStream);
            }
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="obj">需要进行序列化的对象</param>
        /// <returns></returns>
        public static byte[] BinarySerialize(object obj)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, obj);
                memoryStream.Position = 0;
                return memoryStream.GetBuffer();
            }
        }

        #endregion

        #region Xml Serialize

        public static T XmlDeserialize<T>(string strXML)
        {
            using (StringReader sr = new StringReader(strXML))
            {
                XmlSerializer xSer = new XmlSerializer(typeof(T));
                return (T)xSer.Deserialize(sr);
            }
        }

        public static string XmlSerialize(object obj)
        {
            using (TextWriter writer = new EncodedStringWriter(new StringBuilder(), Encoding.UTF8))
            {
                XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
                nameSpace.Add("", "");
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(writer, obj, nameSpace);
                return writer.ToString();
            }
        }

        public static string XmlSerialize(object obj, XmlWriterSettings settings)
        {
            using (TextWriter writer = new EncodedStringWriter(new StringBuilder(), Encoding.UTF8))
            {
                using (XmlWriter streamWriter = XmlWriter.Create(writer, settings))
                {
                    XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
                    nameSpace.Add("", "");
                    XmlSerializer serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(streamWriter, obj, nameSpace);
                    streamWriter.Flush();
                }
                return writer.ToString();
            }
        }

        #endregion
    }
}
